package com.fuluwa.interfaces.knowledge.web;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fuluwa.infra.web.AbstractDomainController;
import com.fuluwa.infra.web.Result;

@Controller
@RequestMapping(value = "/knowledge")
public class FileController extends AbstractDomainController {

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("cid") long cid) {
		try {
			// 初始化目录
			String folderPath = "/data/fuluwa/upload/";
			File fold = null;
			if ((fold = new File(folderPath)).exists()) {
				fold.mkdirs();
			}

			// 拼装文件名称
			String fileName = file.getOriginalFilename();
			String storePath = folderPath + fileName;

			// 转存文件
			File dest = new File(storePath);
			file.transferTo(dest);

			Result result = getSuccessResult();
			result.addData("storePath", storePath);
			return result;
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("File upload Failed:" + e.getMessage());
		}
	}

	@RequestMapping(value = "upload2", method = RequestMethod.POST)
	public ModelAndView upload(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("flag") String flag, ModelMap modelMap) {
		try {
			String path = request.getSession().getServletContext().getRealPath("/");
			path = path.substring(0, path.length() - 1);
			int p = path.lastIndexOf("\\");
			path = path.substring(0, p);
			path = path.replace("\\", "\\/");

			String folderPath = path + "/ftp/upload/";

			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filedata");
			// filedata 是表单的名字

			InputStream stream = file.getInputStream();
			String fileName = file.getOriginalFilename();
			// System.out.println(fileName);
			String fileNameFull = folderPath + fileName;
			OutputStream bos = new FileOutputStream(fileNameFull);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
				bos.write(buffer, 0, bytesRead);
			}

			bos.close();
			stream.close();
			modelMap.put("msg", "添加成功");
		} catch (Exception e) {
			logger.error("");
		}
		return new ModelAndView("/appupload", modelMap);
	}
}
